package com.hc.comm.base;


import java.util.List;

import com.hc.comm.dto.DTO;
import com.hc.comm.mapper.Mapper;
import com.hc.comm.utils.ExceptionUtils;
import com.hc.comm.utils.PagerUtil;

/**
 * 业务基类
 * 
 * @author 
 * @date 2015年12月9日 下午8:27:26
 */
public abstract class BaseServiceImpl<T> implements IBaseService<T> {

	/**
	 * 
	 * @date 2015年12月19日 上午11:55:40
	 * @return IBaseMapper<T>
	 */
	protected abstract Mapper<T> getMapper();

	/*
	 * @param primaryKey
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see
	 * com.by.common.core.service.IBaseService#getByPrimaryKey(java.lang.
	 * Object)
	 */
	@Override
	public T getByPrimaryKey(Object primaryKey) throws Exception {
		return getMapper().getByPrimaryKey(primaryKey);
	}

	/*
	 * @param vo
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see
	 * com.by.common.core.service.IBaseService#getOne(com.by.common.core.base.DTO)
	 */
	@Override
	public T getOne(DTO dto) throws Exception {
		return getMapper().getOne(dto);
	}

	/*
	 * @param vo
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see
	 * com.by.common.core.service.IBaseService#getList(com.by.common.core.base.DTO)
	 */
	@Override
	public List<T> getList(DTO dto) throws Exception {
		return getMapper().getList(dto);
	}

	/*
	 * @param vo
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see
	 * com.by.common.core.service.IBaseService#getListCount(com.by.common.core.base.DTO)
	 */
	@Override
	public Integer getListCount(DTO dto) throws Exception {
		return getMapper().getListCount(dto);
	}

	/*
	 * @param paramMap
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.by.common.core.service.IBaseService#getPagerModel(com.by.common.core.base.DTO)
	 */
	@Override
	public PagerModel<T> getPagerModel(DTO dto) throws Exception {
		return PagerUtil.getPagerModel(new IPagerService<T>() {
			@Override
			public int queryCount(DTO dto) throws Exception {
				return getMapper().getListCount(dto);
			}
			@Override
			public List<T> queryList(DTO dto) throws Exception {
				return getMapper().getList(dto);
			}
		}, dto);
	}

	/*
	 * @param model
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.by.common.core.service.IBaseService#insert(java.lang.Object)
	 */
	@Override
	public T insert(T model) throws Exception {
		getMapper().insert(model);
		return model;
	}

	/*
	 * @param primaryKey
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see
	 * com.by.common.core.service.IBaseService#deleteByPrimaryKey(java.lang.
	 * Object)
	 */
	@Override
	public void deleteByPrimaryKey(Object primaryKey) throws Exception {
		if (getMapper().deleteByPrimaryKey(primaryKey) != 1) {
			ExceptionUtils.throwBaseException("删除数据异常");
		}
	}

	/*
	 * @param model
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see
	 * com.by.common.core.service.IBaseService#updateByPrimaryKey(java.lang.
	 * Object)
	 */
	@Override
	public void updateByPrimaryKey(T model) throws Exception {
		if (getMapper().updateByPrimaryKey(model) != 1) {
			ExceptionUtils.throwBaseException("更新数据异常");
		}
	}

}
